$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/flowActiv/list',
        datatype: "json",
        colModel: [			
			{ label: '流水id', name: 'activId', index: "activeId", width: 45, key: true , sortable: false,align:'center'},
			{ label: '返现时间', name: 'activDate',width: 75,sortable: true,align:'center'},
			{ label: '返现金额', name: 'amount', width: 60, sortable: false,align:'center' },
			{ label: 'SN', name: 'sn', width: 75, sortable: false,align:'center'},
            { label: '返现人id', name: 'agentId', width: 60, sortable: false,align:'center'},
			{ label: '返现人姓名', name: 'name', width: 75, sortable: false,align:'center'},
            { label: '返现人手机号码', name: 'phone', width: 75,align:'center'},
           /* { label: '操作',name:'agentId',formatter:function(value, options, row){
                    return   '<span class = "btn btn-success" onclick = "insert('+value+')"> 新增 </span>&nbsp;&nbsp;'+
                       // '<span class = "btn btn-success" onclick = "toUpdate('+JSON.stringify(row).replace(/"/g, '&quot;')+')"> 修改 </span>&nbsp;&nbsp;'+
                        '<span class="btn btn-danger" onclick="delActiv('+JSON.stringify(row).replace(/"/g, '&quot;')+')">删除</span>&nbsp;&nbsp;';
                }, sortable: false,align:'center'}*/
        ],
		viewrecords: true,
        height: "auto",
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });


});


//进入增加页面
function insert(agentId){
    layer.confirm("确定为此合伙人新增一条返现记录？",function (index) {
        layer.close(index);
        vm.title="新增";
        vm.q.name=null;
        vm.q.phone=null;
        vm.addById=false;
        vm.showList = false;
        vm.showAdd=true;
        vm.flowActiv={
            activId:null,
            activDate:null,
            sn:null,
            amount:null,
            partAmount:null,
            agentId:agentId,
            name:null,
            phone:null

        }
    })
}

//删除记录
function delActiv(row){
    confirm("确定要删除该返现记录？",function(){
        $.ajax({
            type: "POST",
            url: baseURL + "sys/flowactiv/delete",
            dataType: "json",
            data: {"activId":row.activId},
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}

//去修改
function toUpdate(row){

    $.ajax({
        type: "POST",
        url: baseURL + "sys/flowActiv/queryById",
        dataType: "json",
        data: {"activId":row.activId},
        success: function(r){
            if(r.code == 0){
                vm.showList = false;
                vm.showAdd=true;
                vm.addById=false;
                vm.flowActiv=r.flowActiv;
                console.log(r)
                console.log(vm.flowActiv)
                console.log(r.flowActiv)
                vm.q.name=vm.flowActiv.name;
                vm.q.phone=vm.flowActiv.phone;
                //绑定下拉框
                $("#addTime").val(vm.flowActiv.activDate);
            }else{
                alert(r.msg)
            }
        }
    });
}


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            startTime:null,
            endTime:null,
            sn:null,
            name:null,
            phone:null
        },
        showList: true,
        showAdd:false,
        addById:false,
        title:null,
        showUpload:true,
        flowActiv:{
            activId:null,
            activDate:null,
            sn:null,
            amount:null,
            partAmount:null,
            agentId:null,
            name:null,
            phone:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean:function(){
            vm.showList = true;
            vm.addById=false;
            vm.q={
                startTime:null,
                endTime:null,
                sn:null,
                name:null,
                phone:null
            };
            $("#startTime").val(null);
            $("#endTime").val(null);
            $("#jqGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.showAdd=true;
            vm.addById=true;
            vm.title = "新增";
            vm.flowActiv={
                activId:null,
                activDate:null,
                sn:null,
                amount:null,
                partAmount:null,
                agentId:null,
                name:null,
                phone:null
            }
            $("#addTime").val("");

        },
        insert:function(){
            if(isBlank(vm.flowActiv.name)){
                layer.msg("请填写姓名");
                return;
            }
            if(isBlank(vm.flowActiv.phone)){
                layer.msg("请填写手机号码");
                return;
            }
            if(isBlank(vm.flowActiv.amount)){
                layer.msg("请填写返现金额");
                return;
            }
            if(isBlank(vm.flowActiv.sn)){
                layer.msg("请填写终端码");
                return;
            }
            if(isBlank(vm.flowActiv.activDate)){
                layer.msg("请选择时间");
                return;
            }
            $.ajax({
                url: baseURL + "sys/flowActiv/insert",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.flowActiv),
                success: function (r) {
                    if(r.code === 0){
                        alert('操作成功');
                        vm.reload();
                    }else{
                        alert(r.msg);
                    }
                }
            })
        },

        update: function () {
            //vm.flowActiv.activDate=$("#addTime").val();
            $.ajax({
                url: baseURL + "sys/flowActiv/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.flowActiv),
                success: function (r) {
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            })
        },
        reload: function () {
            vm.showList = true;
            vm.showAdd=false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');

            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'startTime': vm.q.startTime,'endTime':vm.q.endTime,'sn':vm.q.sn,"name":vm.q.name,'phone':vm.q.phone},
                page:page
            }).trigger("reloadGrid");
        }
    }
});

